
package com.bangcommunity.bbframe.common.exception;

import com.bangcommunity.bbframe.common.base.BaseResult;
import com.bangcommunity.bbframe.common.base.ResultCode;
import com.bangcommunity.bbframe.common.utils.ToStringUtil;
import com.bangcommunity.bbframe.common.utils.lang.StringUtils;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author huaicheng.thc Date: 2015-11-05 17:18:04
 */
public class ValidateUtil {

    public static void checkNotNull(Object parameter, String msg) {

        if (parameter == null) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    public static void checkNotEmpty(String parameter, String msg) {

        if (StringUtils.isBlank(parameter)) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    public static void checkMaxLength(String parameter, int length, String msg) {

        if (!checkMaxLength(parameter, length)) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    public static boolean checkMaxLength(String parameter, int length) {

        if (parameter == null) {
            return false;
        }
        return parameter.toString().trim().length() <= length;
    }

    public static void checkLength(String parameter, int minLength, int maxLength, String msg) {

        if (!checkLength(parameter, minLength, maxLength)) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    public static boolean checkLength(String parameter, int minLength, int maxLength) {

        if (parameter == null) {
            return false;
        }
        int length = parameter.toString().trim().length();
        return !(length > maxLength || length < minLength);
    }

    public static void checkCharLength(String parameter, int minLength, int maxLength, String msg) {

        if (!checkCharLength(parameter, minLength, maxLength)) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    public static boolean checkCharLength(String parameter, int minLength, int maxLength) {

        parameter = StringUtils.trimToEmpty(parameter);
        int length = ToStringUtil.getChatCount(parameter);
        return !(length > maxLength || length < minLength);
    }

    /**
     * isTrue expression == false 抛异常
     */
    public static void isTrue(Boolean expression, String msg) {

        if (expression == null || !expression) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), msg);
        }
    }

    /**
     * check规则：startDate<=checkDate<=endDate
     */
    public static boolean checkValidDate(Date startDate, Date endDate, Date checkDate) {

        if (startDate == null || endDate == null || checkDate == null) {
            return false;
        }
        return checkDate.getTime() >= startDate.getTime() && checkDate.getTime() <= endDate.getTime();
    }

    /**
     * check规则：min<=max<=cur
     */
    public static boolean checkValidNum(Number min, Number max, Number cur) {

        if (min == null || max == null || cur == null) {
            return false;
        }
        return cur.longValue() >= min.longValue() && cur.longValue() <= max.longValue();
    }

    public static boolean isValidHour(Number hour) {

        if (hour != null) {
            int h = hour.intValue();
            return h >= 0 && h <= 23;
        }
        return false;
    }

    public static boolean isValidMinute(Number minute) {

        if (minute != null) {
            int min = minute.intValue();
            return min >= 0 && min <= 59;
        }
        return false;
    }

    /**
     * 简单判断手机号，
     * 
     * @param mobile
     * @param message
     * @return
     */
    public static void checkSimpleMobile(String mobile, String message) {

        if (null != mobile) {
            Pattern pattern = Pattern.compile("1[0-9]{10}");
            Matcher matcher = pattern.matcher(mobile);
            boolean b = matcher.matches();
            isTrue(b, message);
        }
    }

    public static void checkEmail(String email, String message) {

        if (null != email) {
            Pattern pattern = Pattern
                    .compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
            Matcher matcher = pattern.matcher(email);
            boolean b = matcher.matches();
            isTrue(b, message);
        }
    }

    public static void checkBaseResult(BaseResult baseResult, String defautMsg) {

        if (baseResult == null) {
            throw new BaseRtmException(ResultCode.OTHER.getCode(), defautMsg);
        }
        if (!baseResult.getSuccess()) {
            throw new BaseRtmException(
                    StringUtils.isNotBlank(baseResult.getResultCode()) ? baseResult.getResultCode()
                            : ResultCode.OTHER.getCode(),
                    StringUtils.isNotBlank(baseResult.getMessage()) ? baseResult.getMessage() : defautMsg);
        }
    }

    public static void main(String[] args) {

        // ValidateUtil.checkMaxLength("中文字符拉", 4, "长度超过4了" );
        // System.out.println("胜利拉");
        // System.out.println(checkValidNum(1, 10, 5));
        checkEmail("aaa@jxjr.com", "aaa");
        checkEmail("tanghc@jxjr.com", "aaa");
        checkEmail("tanghc123@jxjr.com", "aaa");
        checkEmail("1231231@jxjr.com", "aaa");
        checkEmail("1231231@aaaa.cccc", "aaa");
        // checkEmail("aaaaa","aaa");
        checkEmail("aaaaa@aaa", "aaa");
    }

}
